package dk.degofedal.scala.network

import java.util.Properties
import java.net.Socket
import java.io.BufferedReader
import java.io.InputStreamReader

import scala.actors.Actor._

class Spider {
  
  
  
}

object Spider {
  
  
  
  def main(args: Array[String]): Unit = { 
    
    var running = true
    
    val consoleOutput = actor {
      // handles output to the console
      println("Console Output actor ready");
      while (running) {
        receive {
          case "quit" => println("Console Output actor quitting");
          case msg => println(msg)
        }
      }
    }
    
    val consoleInput = actor {
      // handles input from the console
      println("Console Input actor ready");
      //val tweetMatcher = """tweet(.+)\n+""".r
      while (running) {
        val input: String = Console.readLine
        input.split(" ").toList match {
          case "quit"::tail =>
            
            // send call of death to other actors
            //avrInput ! "quit"
            //avrOutput ! "quit"
            consoleOutput ! "quit"
            //googleOutput ! "quit"
            //recieveTweet ! "quit"
            //sendTweet ! "quit"
            running = false
          //case "setTime" =>
            //avrOutput ! "CT" + System.currentTimeMillis + "R"
            //case tweetMatcher(msg) => sendTweet ! msg
            
          case "list"::tail =>
            handleList(tail)
          case _ =>
            //avrOutput ! input
            consoleOutput ! input
        }
      }
      println("Console Input actor quitting");
    }  
    
    /*assert(args.length == 2)
    
    val ipNet = IpAddress(args(0))
    val ipMask = IpNetworkMask(args(1))
    
    val ipNetwork = new IpNetwork(ipNet, ipMask)
    ipNetwork.addresses() foreach(checkHost(_))*/
    
    /*val jvmProperties = System.getProperties();
    val propNames = jvmProperties.propertyNames()
    
    while(propNames.hasMoreElements()) {
      val propName: String = propNames.nextElement().asInstanceOf[String]
      val propValue = jvmProperties.getProperty(propName)
      println(propName + " = " + propValue)
    }*/
  }
  
  def handleList(args: List[String]): Unit = {
    args match {
      case _ => println(args)
    }
  }

  def checkHost(host: IpAddress): Unit = {
    if(host.toInetAddress.isReachable(1000)){
    	println(host)
    	val process = Runtime.getRuntime().exec("sudo nmap -sS -O "+host.toString());
    	val input = new BufferedReader (new InputStreamReader(process.getInputStream()));
    	val error = new BufferedReader (new InputStreamReader(process.getErrorStream()));
    	var line = error.readLine()
    	while (line != null) {
    		System.out.println(line);
    		line = error.readLine()
    	}
    	input.close();
    	error.close()
    }
  }
  
}